/*!
 * \file MessageQueue.h
 * \brief
 */

#ifndef MESSAGE_QUEUE_H
#define MESSAGE_QUEUE_H

#include <list>
#include <boost/thread/mutex.hpp>
#include "RequestHandler.h"
#include "Message.h"

namespace tsc
{
namespace common
{

class MessageQueue
{
public:
  static MessageQueue* instance();
  ~MessageQueue();

  void registerServerHandler(RequestHandler* aHandler);
  void registerApplicationHandler(RequestHandler* aHandler);
  // Server is usually in the listening state and will send replies form the Out
  // Queue only if sometnig came from the client.
  // This function forces the Server to send a message from Out Queue, if applicable.
  void forceServerToSend();

  void toInQueue(Message*& aMessage);
  void toOutQueue(Message*& aMessage);
  void toTimerQueue(Message*& aMessage);
  bool fromInQueue(Message*& aMessage);
  bool fromInQueue(Message*& aMessage, unsigned int aConnectionId);
  bool fromOutQueue(Message*& aMessage);
  bool fromOutQueue(Message*& aMessage, unsigned int aConnectionId);
  bool fromTimerQueue(Message*& aMessage, unsigned int aTimerId);

protected:
  MessageQueue();

public:
  static MessageQueue* mInstance;
  boost::mutex         mMutex;

  std::list<Message*>  mInQueue;
  std::list<Message*>  mOutQueue;
  std::list<Message*>  mTimerQueue;

  // Request handler form Server
  RequestHandler* mServerHandler;
  // Request handler form Aplication
  RequestHandler* mApplicationHandler;

};

} // namespace common
} // namespace tsc

#endif // MESSAGE_QUEUE_H
